import Vue from 'vue'
export default {
  SET_CONNECTING(state, flag) {
    state.connecting = flag
  },
  SET_CONNECTED(state, flag) {
    state.connected = flag
  },
  SET_PEER(state, data) {
    if (state.peers.hasOwnProperty(data.id)) {
      state.peers[data.id] = data
    } else {
      Vue.set(state.peers, data.id, data)
    }
  },
  SET_WEBCAM_IN_PROGRESS(state, flag) {
    state.webcamInProgress = flag
  },
  SET_ME_PRODUCER(state, producer) {
    state.producer.push(producer)
  },
  SET_ROOM_CLIENT(state, obj) {
    state.roomClient = obj
  },
  SET_CONSUMER(state, obj) {
    const peerId = obj.peerId
    const consumer = obj.data
    if (state.consumers.hasOwnProperty(peerId)) {
      state.consumers[peerId].push(consumer)
    } else {
      Vue.set(state.consumers, peerId, [consumer])
    }
  },
  SET_USE_TYPE(state, type) {
    state.useType = type
  },
  SET_ME_CAN_VIDEO(state, flag) {
    state.me.canVideo = flag
  },
  SET_ME_CAN_AUDIO(state, flag) {
    state.me.canAudio = flag
  },
  SET_ME_PEER_ID(state, peerId) {
    state.me.peerId = peerId
  },
  SET_ROOM_ID(state, id) {
    state.roomId = id
  },
  REMOVE_PEER(state, peerId) {
    const result = {}
    if (state.peers.hasOwnProperty(peerId)) {
      for (let item in state.peers) {
        if (state.peers.hasOwnProperty(item)) {
          if (item !== peerId) {
            result[item] = state.peers[item]
          }
        }
      }
      state.peers = result
    }
  },
  REMOVE_CONSUMER(state, obj) {
    const peerId = obj.peerId
    const consumerId = obj.consumerId
    if (state.consumers.hasOwnProperty(peerId)) {
      let arr = state.consumers[peerId]
      let temp = []
      arr.forEach((val) => {
        if (val.id !== consumerId) {
          temp.push(val)
        }
      })
      if (temp.length > 0) {
        Vue.set(state.consumers, peerId, temp)
      } else {
        let temp = {}
        for (let item in state.consumers) {
          if (state.consumers.hasOwnProperty(item)) {
            if (item !== peerId) {
              temp[item] = state.consumers[item]
            }
          }
        }
        Vue.set(state.consumers, peerId, temp)
      }
    }
  },
  SET_HOMEROWNER(state, peerId) {
    state.homeowner = peerId
  },
  SET_JOIN_ROOM(state, flag) {
    state.canJoinRoom = flag
  },
  SET_DATA_CONSUMER(state, data) {
    state.dataConsumers = data
  },
  SET_CALL_TYPE(state, type) {
    state.callType = type
  },
  SET_PEERS_ID(state, arr) {
    state.peersId = arr
  }
}
